<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-column.html">« array_column</a></li>
      <li style="float: right;"><a href="function.array-count-values.html">array_count_values »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-combine" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_combine</h1>
    <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_combine</span> &mdash; <span class="dc-title">
     创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-combine-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>array_combine</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$keys</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$values</code></span>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     返回一个 <span class="type">array</span>，用来自
     <code class="parameter">keys</code> 数组的值作为键名，来自
     <code class="parameter">values</code> 数组的值作为相应的值。
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.array-combine-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">keys</code></dt>

     <dd>

      <p class="para">
       将被作为新数组的键。非法的值将会被转换为字符串类型（<span class="type">string</span>）。
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">values</code></dt>

     <dd>

      <p class="para">
       将被作为 <span class="type">Array</span> 的值。
      </p>
     </dd>

    

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-combine-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回合并后的 <span class="type">array</span>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.array-combine-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   自 PHP 8.0.0 起，如果 <code class="parameter">keys</code> 和 <code class="parameter">values</code> 的元素数量不同，将会抛出
   <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>。在 PHP 8.0.0 之前，会引发 <strong><code>E_WARNING</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-combine-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       如果每个数组的元素数量不相等，现在 <span class="function"><strong>array_combine()</strong></span> 将会抛出 <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>，之前此方法返回 <strong><code>false</code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-combine-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4899">
    <p><strong>示例 #1 一个 <span class="function"><strong>array_combine()</strong></span> 简单的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'avocado'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_combine</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [green]  =&gt; avocado
    [red]    =&gt; apple
    [yellow] =&gt; banana
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-combine-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-merge.html" class="function" rel="rdfs-seeAlso">array_merge()</a> - 合并一个或多个数组</span></li>
    <li class="member"><span class="function"><a href="function.array-walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - 使用用户自定义函数对数组中的每个元素做回调处理</span></li>
    <li class="member"><span class="function"><a href="function.array-values.html" class="function" rel="rdfs-seeAlso">array_values()</a> - 返回数组中所有的值</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>